﻿using UnityEngine;
using System.Collections;

public class ShipManager : MonoBehaviour {
	public static ShipManager instance;
	public GUIText guiFuel;
	public GUIText distanceFromFloor;
	public GUIText healGUI;

	public GameObject shipPrefab;
	public Ship currentShip;
	public SimpleFollow cameraFollow;
	public float timeOut=2;
	private float timeOutCooldown=0;
	public Transform currentSpawn;

	void Start(){
		instance = this;
		}
	void Update () {

		if (currentShip == null) {


			timeOutCooldown += Time.deltaTime;
			if (timeOutCooldown >= timeOut) {
				GameObject go = GameObject.Instantiate (shipPrefab, currentSpawn.position, Quaternion.identity)as GameObject;
				cameraFollow.target = go.transform;
				currentShip = go.GetComponent<Ship> ();
				timeOutCooldown = 0;
			}
		} else {
			guiFuel.text = ((int)(currentShip.fuel)).ToString ();
			healGUI.text = ((int)currentShip.health).ToString ();
			Ray ray = new Ray (currentShip.transform.position, -Vector3.up);
			RaycastHit hit;
			
			if (Physics.Raycast (ray, out hit, Mathf.Infinity)) 
			{
				distanceFromFloor.text = ((int)hit.distance).ToString();
			}

		}
	}
}
